package wsh;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Scanner; 
public class GoldenPoint {
public static void main(String[] args){
    GoldenPoint gd=new GoldenPoint();
    gd.goldPoint();
}
public void goldPoint(){
    HashMap<String,Double> inputMap=new HashMap<String,Double>();
    HashMap<String,Double> scoreMap=new HashMap<String,Double>();
    String name="";
    Double inputScore;
    int time;
    Double sum=0.0;
    Double aver=0.0;
    Scanner scan=new Scanner(System.in);
    for(int i=0;i<3;i++){
        System.out.println("请输入第"+(i+1)+"个参加者的姓名：");
        name=scan.next();
        System.out.println("请输入第一轮的数字：");
        System.out.println("数字范围在0~100！");
        inputScore=scan.nextDouble();
        if (inputScore<0||inputScore>100)
        {System.out.println("输入数字错误");
        break;}
        else
         inputMap.put(name, inputScore);
         scoreMap.put(name,(double) 0);
         sum+=inputScore;
    }
    aver=sum/3*0.618;
    System.out.println("aver="+aver);
    this.findWinner(inputMap, scoreMap, aver,3);
    this.show(scoreMap);
            
    
    System.out.println("游戏结束");
}
public void findWinner(HashMap<String,Double> inputMap,HashMap<String,Double> scoreMap,Double aver,int renshu){    
    Double temp;
    Double temp0;
    List<String> latest=new ArrayList<String>();
    List<String> farthest=new ArrayList<String>();
    Iterator iter = inputMap.entrySet().iterator();
    Map.Entry entry = (Map.Entry) iter.next();
    Double input = (Double) entry.getValue();
    String key0 = (String) entry.getKey();
    latest.add(key0);
    farthest.add(key0);
    temp0=temp=Math.abs(aver-input);
    while (iter.hasNext()) {    
        entry = (Map.Entry) iter.next();
        String key = (String) entry.getKey();
        input = (Double) entry.getValue();
        Double temp1=Math.abs(aver-input);
        if(temp>temp1){
            temp=temp1;
             latest.clear();
             latest.add(key);
        }else if(temp==temp1){
            latest.add(key);
        }
        if(temp0<temp1){
            temp0=temp1;
            farthest.clear();
            farthest.add(key);}
        else if(temp0==temp1){
            farthest.add(key);
        }
    }
    iter = scoreMap.entrySet().iterator();
    while (iter.hasNext()) {
        Map.Entry entry0 = (Map.Entry) iter.next();
        String key = (String) entry0.getKey();
        Double score =(Double) entry0.getValue();
        if(this.containList(key, latest)){
            score=score+renshu;
            scoreMap.put(key, score);
            }
        if(this.containList(key, farthest)){
            score=score-2;
            scoreMap.put(key, score);
        }
        }
}
public boolean containList(String str,List<String> list){
    for(int i=0;i<list.size();i++){
        if(str.equals(list.get(i))){
            return true;
        }
    }
    return false;
}
public void show(HashMap<String,Double> scoreMap){
    System.out.println("得分情况：");
    Iterator iter = scoreMap.entrySet().iterator();
    while (iter.hasNext()) {
        Map.Entry entry0 = (Map.Entry) iter.next();
        String key = (String) entry0.getKey();
        Double score =(Double) entry0.getValue();
        System.out.println(key+"："+score);
    }
}
}